package com.p2p.account.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.p2p.account.po.Account;
import com.p2p.account.service.AccountService;
import com.p2p.account.vo.AccountSelect;
import com.p2p.common.base.BaseController;
import com.p2p.common.constants.Message;
import com.p2p.common.enums.ResponseEnum;
import com.p2p.common.utils.MD5Encryption;
import com.p2p.common.utils.StringUtils;
import com.p2p.common.utils.Underline2Camel;
import com.p2p.port.po.MonitorPort;
import com.p2p.user.service.UserRoleService;
import com.p2p.wallet.po.Wallet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Allen on 2017/3/19.
 */
@RestController
@RequestMapping(value = "account")
public class AccountController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(AccountController.class);
    @Autowired
    private AccountService accountService;
   
    /**
     * 获取用户列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "selectAccount", method = RequestMethod.GET)
    public Map selectAccount(HttpServletRequest request) {
   String agentid=null;
   String coinid=null;
   String  poolContent=null;
   if(StringUtils.isNotEmpty(request.getParameter("agentId"))){
	   if(!request.getParameter("agentId").equals("0"))
	   {
		   agentid=request.getParameter("agentId");
	   }
	  
   }
   if(StringUtils.isNotEmpty(request.getParameter("coinId"))){
	   if(!request.getParameter("coinId").equals("0"))
	   {
		   coinid=request.getParameter("coinId");
	   }
	   
	   
   }
   if(StringUtils.isNotEmpty(request.getParameter("poolContent"))){
	   if(!request.getParameter("poolContent").equals("0"))
	   {
		   poolContent=request.getParameter("poolContent");
	   }
	   
	   
   }
 
        Integer pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String sortField = Underline2Camel.camel2Underline(request.getParameter("sort") == null ? "id" : request.getParameter("sort"), true);
        String orderField = request.getParameter("order") == null ? "desc" : request.getParameter("order");

        int startRow = (pageIndex-1) * pageSize;
        List<Account> userList = accountService.selectLists(startRow, pageSize, sortField, orderField,agentid,coinid,poolContent);

        int size = accountService.selectList(new EntityWrapper<Account>().eq("status", 1)).size();

        Map result = new HashMap();
        result.put("rel", true);
        result.put("msg", "SUCCESS");
        result.put("list", userList);
        result.put("count", size);
        return result;
    }
    @RequestMapping(value = "updateAccount", method = RequestMethod.POST)
	public Message updateAccount(HttpServletRequest request, Account wallet) {
    	System.out.println("调用修改接口");
    	accountService.updateById(wallet);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
	}

	/**
	 * 逻辑删除用户
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteAccount", method = RequestMethod.GET)
	public Message deleteAccount(HttpServletRequest request) {
		Long Id = Long.parseLong(request.getParameter("accountid"));
		Account account = accountService.selectById(Id);
		account.setStatus(-1);
		accountService.updateById(account);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
	}
    /**
     * 增加用户列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "insertAccount", method = RequestMethod.POST)
    public Message insertAccount(HttpServletRequest request, Account account) {
    	
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
   account.setStatus(1);
            account.setCreateTime(timestamp);
       System.out.println("状态"+account.getStatus());
        accountService.insert(account);
        return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }
    @RequestMapping(value = "selectAccountById", method = RequestMethod.GET)
	public Message selectAccountrById(HttpServletRequest request) {
		Long id = Long.parseLong(request.getParameter("id"));
		Account account=	accountService.selectById(id);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), account);
	}
}
